/**
 * @class common.base.popover
 * @extends common.base.object
 * popover基类
 */
define(function(require) {
    var BaseObject = require('common/base/object');

    return BaseObject.extend({
        /**
         * 构造函数
         * @param  {Object} params 配置参数
         */           
        initialize: function(params) {
            this.params = params;
            if (typeof params.target == 'string') {
                params.target = $(params.target);
            }
            this.$target = params.target;
            this.html = this.render(this.tpl, params.data);
            this.show();
        },
        /**
         * 显示popover
         */           
        show: function() {
            var _this = this,
                position = _this.params.position;
            _this.el = "#" + _this.id;
            var offset = this.$target.offset(),
                tWidth = this.$target.width(),
                tHeight = this.$target.height();
            var name = this.name;

            $('body').append("<div class='popover " + this.params.position + "' id=" + _this.id + " style='z-index:" + (window.Z_INDEX++) + "'>" +
                "<div class='arrow'></div>" +
                "<div class='popover-content " + name + "Popover'>" + this.html + "</div></div>");

            _this.popover = $(_this.el);

            offset = _this.getOffset(position, offset, tWidth, tHeight, _this.popover);
            _this.popover.css({
                'display': 'block',
                'top': (offset.top < 0 ? 20 : offset.top) + 'px',
                'left': (offset.left < 0 ? 20 : offset.left) + 'px'
            });
            $('.cancel,.pop_close', this.popover).click(function() {
                _this.close();
            });
            var $input = $('input[type="text"],textarea', this.popover);
            if ($input.size() > 0) {
                this.setSelectionRange($input[0], $input.val().length);
            }
            this.bindEvents();
            $('body').click(function(event) {
                if ($(_this.el).size() == 0) {
                    return;
                }
                if (!$(_this.el)[0].contains(event.target) && !_this.$target[0].contains(event.target)) {
                    _this.close();
                }
            });
        },
        getOffset: function(position, offset, tWidth, tHeight, $popover) {
            switch (position) {
                case 'right':
                    offset.top = offset.top + tHeight / 2 - $popover.height() / 2;
                    offset.left = offset.left + tWidth + 10;
                    break;
                case 'bottom':
                    offset.top = offset.top + tHeight + 5;
                    offset.left = offset.left + tWidth / 2 - $popover.width() / 2;
                    break;
                case 'left':
                    offset.top = offset.top + tHeight / 2 - $popover.height() / 2;
                    offset.left = offset.left - $popover.width() - 10;
                    break;
                case 'top':
                default:
                    offset.top = offset.top - $popover.height() - 10;
                    offset.left = offset.left + tWidth / 2 - $popover.width() / 2;
            }

            return offset;
        },
        bindEvents: function() {},
        /**
         * 关闭popover         
         */          
        close:function(){
            this.popover.remove();
        }
    });
});